(function($) {
	$.getUrlParam = function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}
})(jQuery);



const d = D = {
	queryOne() {
		var id = $.getUrlParam("did");
		$.ajax({
			type: 'get',
			url: url.path + 'dingdan/queryOne',
			data: {
				id: id
			},
			success: function(res) {
				var res = eval("(" + res + ")");
				if (res.code == "200") {
					$('#uid').val(res.data.uid);
					$('#woNumber').text(res.data.dno);
					$('#contactNameDetail').text(res.data.lxrname);
					$('#contactPhoneDetail').text(res.data.lxrphone);
					$('#faultLocationDetail').text(res.data.address);
					$('#faultReasonDetail').text(res.data.description);
					$('#faultImage1').attr('src', res.data.img1);
					$('#faultImage2').attr('src', res.data.img2);
					$('#faultImage3').attr('src', res.data.img3);
					$('#repairDateDetail').text(D.timestampToDate(res.data.date));
					$('#faultLocationDetail').text(res.data.address);
					$('#wname').text(res.data.wname);
					$('#wid').val(res.data.wid);
					$('#wphone').text(res.data.wphone);
				} else {
					alert(res.msg);
					$('#captchaContainer').data('sliderCaptcha').reset();
					$('#captchaContainer').data('sliderCaptcha').init();
					$('#xyb').attr('disabled', true).css({
						'background-color': '#ccc',
						'cursor': 'not-allowed',
						'border-color': '#ccc'
					});
				}
			}
		});
	},
	timestampToDate(timestamp) {
		const date = new Date(timestamp); // 将时间戳转换为Date对象
		const options = {
			year: 'numeric',
			month: 'long',
			day: 'numeric'
		}; // 定义日期格式
		return new Intl.DateTimeFormat('zh-CN', options).format(date); // 使用Intl.DateTimeFormat进行格式化
	},
	edit(state) {
		var currentDate = new Date();
		// 获取年份
		var year = currentDate.getFullYear();
		// 获取月份（注意：月份从 0 开始计数，所以要加 1）
		var month = currentDate.getMonth() + 1;
		// 获取日期
		var day = currentDate.getDate();
		// 将日期格式化为 "YYYY-MM-DD"
		var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
		var isAdd = confirm("是否确认执行当前操作?")
		if (isAdd) {
			$.ajax({
				url: url.path + 'dingdan/edit',
				data: {
					id: $.getUrlParam("did"),
					state: state,
					wid: $('#wid').val(),
					wname: $('#wname').text(),
					wphone: $('#wphone').text(),
					enddate: "",
					senddate: formattedDate
				},
				type: 'post',
				dataType: 'json',
				success: function(res) {
					if (res.code == 200) {
						alert(res.msg);
						window.location.href = "yiquxiao.html?did=" + $.getUrlParam("did");

					}
				},
				error: function() {
					alert('修改失败')
				}
			})
		}
	},
	queryCostList() {
		var _clist = $('#_tbody');
		var did = $.getUrlParam("did");
		var uid = $('#uid').val();
		var zongji = 0;
		zongji = parseInt(zongji);
		$.ajax({
			type: 'get',
			url: url.path + 'cost/getCostList',
			data: {
				uid: uid,
				did: did
			},
			dataType: 'json',
			success: function(res) {
				if (res.code == 200) {
					_clist.empty(); // 将容器清空
					let list = res.data;
					$(list).each(function(i, e) {
						let heji = e.fprice * e.num;
						let _tr = $(`<tr></tr>`)
						let _tds = $(`
									<td>` + e.fname + `</td>
						            <td>` + e.fd + `</td>
						            <td>` + e.fprice + `</td>
									<td>` + e.num + `</td>
									<td>` + heji + `</td>
						    `);
						zongji = zongji + parseInt(heji);
						_tr.append(_tds);
						_clist.append(_tr);
					});
					let zong = $(`<td style="color: red;" id='zongji'>` + zongji + `</td>`);
					$('#_tr').append(zong)
				}
			},
			error() {}
		});
	}
}

$(function() {
	// 重新加载页面
	D.queryOne();
	D.queryCostList();
	$('#cancelWorkorderBtn').click(function() {
		D.edit(7);
	})
})